package com.donleo.stmg.relation.controller;


import com.donleo.stmg.common.api.CommonResult;
import com.donleo.stmg.relation.model.UserLoginRecord;
import com.donleo.stmg.relation.service.EsUserLoginRecordService;
import com.donleo.stmg.relation.service.UserLoginRecordService;
import com.donleo.stmg.relation.vo.EsUserLoginRecordVo;
import com.donleo.stmg.relation.vo.UserLoginRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 用户登录记录表 前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/userLoginRecord")
public class UserLoginRecordController {

    @Autowired
    private UserLoginRecordService userLoginRecordService;

    @Autowired
    private EsUserLoginRecordService esUserLoginRecordService;
    /**
     * 查询所有
     */
    @PreAuthorize("hasAuthority('method:log:read')")
    @GetMapping("/findAll")
    public CommonResult getAllLoginRecord(UserLoginRecordVo userLoginRecordVo){
        List<UserLoginRecord> list=userLoginRecordService.getAllLoginRecord(userLoginRecordVo);
        long total = userLoginRecordService.getCount(userLoginRecordVo);
        return CommonResult.success(total,list);
    }

    /**
     * 批量删除
     */
    @PreAuthorize("hasAuthority('method:log:delete')")
    @PostMapping("/deletes")
    public CommonResult deletes(Integer[] ids){
        List<Integer> idsList = Arrays.asList(ids);
        return userLoginRecordService.deletes(idsList);
    }

    /**
     * 导入es
     * @return
     */
    @GetMapping("/importAll")
    public CommonResult importAll(){
        int count = esUserLoginRecordService.importAll();
        return CommonResult.success(count);
    }

    /**
     * 搜索
     * @param userName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/search")
    public CommonResult search(String userName,Integer pageNum,Integer pageSize){
        Page<EsUserLoginRecordVo> search = esUserLoginRecordService.search(userName, pageNum, pageSize);
        return CommonResult.success(search);
    }

    /**
     * 高亮搜索
     * @param userName 用戶名
     * @return  SearchHits
     */
    @GetMapping("/searchHit")
    public CommonResult searchHit(String userName){
        SearchHits searchHits = esUserLoginRecordService.searchHit(userName);
        return CommonResult.success(searchHits);
    }

    /**
     * 高亮搜索
     * @param userName
     * @return
     */
    @GetMapping("/transferSearchHit")
    public CommonResult transferSearchHit(String userName){
        return esUserLoginRecordService.transferSearchHit(userName);
    }

    /**
     * 刪除Es
     * @param id
     * @return
     */
    @GetMapping("/deleteEsById")
    public CommonResult deleteEs(Integer id){
        esUserLoginRecordService.delete(id);
        return CommonResult.success(1);
    }
}
